<?php

namespace app\api\controller;
use app\common\controller\Api;
use app\util\CurlPush;
require '../extend/jpush/autoload.php';
use JPush\Client as JPush;


class Push extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    /*
     * 更新设备标识
     * @path api/push/getRegistration
     * */
    public function getRegistration()
    {
        $user = $this->auth->getUser();
        $type = $this->request->post('type');
        if(!$type){
            $this->error("设备类型不能为空");
        }
        $registration_id = $this->request->post('registration_id');
        if(!$registration_id){
            $this->error("设备标识不能为空");
        }
        if($type == 2 && !empty($registration_id)){
            $user->android_registration_id = $registration_id;
        }
        if($type == 3 && !empty($registration_id)){
            $user->ios_registration_id = $registration_id;
        }
        $user->save();
        $this->success("设备标识更新成功");
    }

    public function iosPush($registration_id=[],$alert=''){
        $app_key = '1a870eedfe0edd1211277114';
        $master_secret = 'a9521dfc9e1ed7dcaa70e8da';
        $client = new JPush($app_key, $master_secret);
        // 完整的推送示例,包含指定Platform,指定Alias,Tag,指定iOS,Android notification,指定Message等
        $result = $client->push()
            ->setPlatform(array('ios','android'))
            //->addAlias('alias1')
            //->addTag(array('tag1', 'tag2'))
            ->setNotificationAlert('Hi, JPush')
            ->addRegistrationId($registration_id)
            ->iosNotification($alert, array(
                'badge' => '+1', // 设置应用图标角标增加1，也可以是一个具体数字，例如 '5'
                'sound' => 'default', // 设置通知声音，'default' 表示系统默认声音
            ))
            //->addAndroidNotification('Hi, android notification', 'notification title', 1, array("key1"=>"value1", "key2"=>"value2"))
            //->addIosNotification("Hi, iOS notification", 'iOS sound', 1999, true, 'iOS category', array("key1"=>"value1", "key2"=>"value2"))
            //->setMessage("msg content", 'msg title', 'type', array("key1"=>"value1", "key2"=>"value2"))
            ->setOptions(time(), 3600, null, true)
            ->send();

        echo 'Result=' . json_encode($result) ;

    }

    public function androidPush($registration_id=[],$alert='')
    {
        $app_key = '1a870eedfe0edd1211277114';
        $master_secret = 'a9521dfc9e1ed7dcaa70e8da';
        $client = new JPush($app_key, $master_secret);
        $push = $client->push();
        $cid_res = $push->getCid($count = 1, $type = 'push');
        $cid = $cid_res['body']['cidlist'][0];
        $platform = array('android','ios');
        $regId = $registration_id;
        $android_notification = array(
            'title' => $alert,
            'builder_id' => 1,
            'badge_add_num' => 1, // 设置应用图标角标增加1
            // 'badge_set_num' => 0, // 设置应用图标角标显示1
            'badge_class' => 'io.dcloud.PandoraEntry',
            'display_foreground' => '1',
            'extras' => array(
                'type' => '',
                'id' => 2
            ),
        );
        $ios_notification = array(
            "alert"=>"hello, JPush!",
            "sound"=>"default",
            "badge"=>"+1",
            "extras"=>array(
                "news_id"=>"12",
                "my_key"=>"a value",
            ),
        );
        $options = [
            'classification' => 1,
            'apns_production' => false,
            'third_party_channel' => [
                'huawei' => [
                    // "distribution" => "secondary_push",
                    // "distribution_fcm" => "jpush", 
                    // "distribution_customize" => "secondary_push", 
                    // "category" => "IM",
                    // "sound" => "/raw/shake",
                    // "default_sound" => true,
                    // "importance" => "NORMAL",
                    "target_user_type" => 1,
                    // "style" => 2, 
                    // "only_use_vendor_style" => false,
                ],
            ],
        ];
        $content = $alert;
        $push->setCid($cid)
            ->setPlatform($platform)
            ->addRegistrationId($regId)
            ->androidNotification($content, $android_notification)
            ->iosNotification($content,$ios_notification)
            ->options($options)
            //->message($content,$android_notification)
            ->send();
    }

}
